local sk__fujian = fk.CreateSkill {

  name = "sk__fujian",

  tags = {},

}


sk__fujian:addEffect(fk.EventPhaseStart, {
  name = "sk__fujian",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__fujian.name) and player.phase == Player.Start and
        table.find(player.room:getOtherPlayers(player), function(p)
          return not p:isKongcheng()
        end)
  end,
  on_cost = function(self, event, target, player, data)
    local targets = table.filter(player.room:getOtherPlayers(player), function(p)
      return not p:isKongcheng()
    end)
    local to=player.room:askToChoosePlayers(player,{
      targets=targets,
      min_num=1,
      max_num=1,
      skill_name=sk__fujian.name,
      prompt="#sk__fujian-choose",
      cancelable=true,
    })
    if #to > 0 then
      event:setCostData(self, { tos = to })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local card, _ = room:askToChooseCardsAndChoice(player, {
      cards = to:getCardIds("h"),
      choices = { "OK" },
      skill_name = sk__fujian.name,
      prompt = "#sk__fujian-mark::" .. to.id,
      cancel_choices = {},
      min = 1,
      max = 1,
    })
    room:setCardMark(Fk:getCardById(card[1]), "@sk__fujian", "0")
    room:setCardMark(Fk:getCardById(card[1]), "sk__fujian_info", { player.id, to.id, 0 })
  end,
})
sk__fujian:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return table.find(player:getCardIds("h"), function(id)
        return Fk:getCardById(id):getMark("sk__fujian_info") ~= 0
      end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getCardIds("h")) do
      local card = Fk:getCardById(id)
      if card:getMark("sk__fujian_info") ~= 0 then
        if card:getMark("@sk__fujian") == "0" then
          room:setCardMark(card, "@sk__fujian", 1)
        else
          room:addCardMark(card, "@sk__fujian", 1)
        end
        local mark = card:getMark("sk__fujian_info")
        mark[3] = mark[3] + 1
        room:setCardMark(card, "sk__fujian_info", mark)
      end
    end
  end,
})

sk__fujian:addLoseEffect(function(self, player, is_death)
  local room = player.room
  for _, id in ipairs(Fk:getAllCardIds()) do
    local card = Fk:getCardById(id)
    local mark = card:getMark("sk__fujian_info")
    if mark ~= 0 and mark[1] == player.id then
      room:setCardMark(card, "@sk__fujian", 0)
      room:setCardMark(card, "sk__fujian_info", 0)
    end
  end
end)


sk__fujian:addEffect(fk.Deathed, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return player:hasSkill(self, true, true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      local mark = card:getMark("sk__fujian_info")
      if mark ~= 0 and mark[1] == player.id then
        room:setCardMark(card, "@sk__fujian", 0)
        room:setCardMark(card, "sk__fujian_info", 0)
      end
    end
  end,
})

sk__fujian:addEffect(fk.AfterCardsMove, {
  name = "#sk__fujian_trigger",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("sk__fujian_info") ~= 0 then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("sk__fujian_info") ~= 0 then
            local card = Fk:getCardById(info.cardId)
            local mark = card:getMark("sk__fujian_info")
            room:setCardMark(card, "@sk__fujian", 0)
            local src = room:getPlayerById(mark[1])
            if not src.dead and src:hasSkill(sk__fujian.name) then
              room:doIndicate(mark[1], { mark[2] })
              src:broadcastSkillInvoke("sk__fujian")
              room:notifySkillInvoked(src, "sk__fujian", "offensive")
              if not player.dead then
                room:loseHp(player, 1, "sk__fujian")
              end
              if not src.dead and mark[3] > 0 then
                src:drawCards(mark[3], "sk__fujian")
              end
            end
            room:setCardMark(card, "sk__fujian_info", 0)
          end
        end
      end
    end
  end,
})

return sk__fujian
